﻿using System;
using System.Windows.Input;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.Commands
{
	public class Command : ICommand
	{
		public event EventHandler CanExecuteChanged;

		Predicate<Object> canExecute;
		Action<Object> executeAction;

        public Command(Predicate<Object> canExecute, Action<object> executeAction)
		{
			this.canExecute = canExecute;
            this.executeAction = executeAction;
		}


		public bool CanExecute(object parameter)
		{
			if (canExecute != null)
				return canExecute(parameter);

			return true;
		}

		public void UpdateCanExecuteState()
		{
			if (CanExecuteChanged != null)
				CanExecuteChanged(this, new EventArgs());
		}

		public void Execute(object parameter)
		{
			if (executeAction != null)
				executeAction(parameter);

			UpdateCanExecuteState();
		}
	}
}
